package lucene.server.web.dao;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import lucene.server.web.model.LoginUser;

public class UserDao {

    @SneakyThrows
    public static int modifyToken(LoginUser user){
        return  Db.use().update(
                Entity.create().set("token",  user.getToken()),
                Entity.create("user").set("username", user.getUsername())
        );
    }
    @SneakyThrows
    public static LoginUser checkToken(String token){
        Entity entity = Db.use().get(Entity.create("user").set("token", token));
        if (entity == null || entity.isEmpty()){
            return null;
        }else {
            return entity.toBean(LoginUser.class);
        }
    }
    @SneakyThrows
    public static LoginUser getUser(String userName){

        Entity entity = Db.use().get(Entity.create("user").set("username", userName));
        if (entity == null || entity.isEmpty()){
            return null;
        }else {
            return entity.toBean(LoginUser.class);
        }
    }
}
